import Vue from 'vue'
import Router from 'vue-router'
// import HelloWorld from '@/components/HelloWorld'
import Home from "../views/Home";
import register from "../views/register";
import Login from "../views/Login";
import userinfo from "../views/userinfo";
import edit from "../views/edit";
import Article from "../views/Article";
Vue.use(Router)



const router =new Router({
  mode:'history',
  routes: [
    {
      path: '/',
      component: Home,
      meta: {
        keepalive:true
      }
    },
    {
      path: "/register",
      component: register
    },
    {
      path: '/login',
      component: Login
    },
    {
      path: '/userinfo',
      component: userinfo,
      meta:{
        istoken:true
      }
    },
    {
      path: '/edit',
      component: edit,
      meta:{
        istoken: true
      },
    },
    {
      path: '/article/:id',
      component: Article
    }
  ]
})

router.beforeEach((to, from, next) => {
  if(!localStorage.getItem('token')&&!localStorage.getItem('id')&&to.meta.istoken===true) {
    router.push('/login')
    Vue.prototype.$msg.fail('请重新登录')
    return
  }
  next()
})

export default router


